<?php
namespace Home\Controller;
use Lib\Alidayu\SendSMS;
class SendorderController extends CommonController {

public function _initialize()
{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("send");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
}
//待处理派发订单列表
public function getsendorder_list()
{
		// p($_POST);exit();
		$db_order = M("order");
		$ordertype = I('get.ordertype');
		$soundtype = I('get.soundtype');
		$order_name = I('get.order_name');
		$this->ordertype = $ordertype;
		$this->soundtype = $soundtype;
		$this->order_name = $order_name;
		$where = "type=4 ";
		if (!empty($soundtype)) {$where .= " and category = \"".($soundtype)."\" ";}
		if (!empty($order_name)) {$where .= " and (id like '%$order_name%' or title like '%$order_name%')";}
		if ($ordertype == 1) {$where .= " and isimportant=1 ";}
		// echo $where;exit();
		$adminid = urlsafe_b64decode(cookie('heardadminData'));
		$sex = M('admin')->where("heard_admin_id = $adminid")->getField('sex');
		$where .= " and (status=1)";
		$where .= " and (sex in ('0',$sex))";
		$count = $db_order->where($where)->count();
		$p = getpage($count, 20);
		$orderinfo = $db_order->where($where)->order("isimportant=1 desc,id desc")->limit($p->firstRow, $p->listRows)->select();
		foreach ($orderinfo as $key => &$value) {
			$value['pay_time'] = $db_order->where(array("id"=>$value['id']))->order("id asc")->find()['pay_time']+1800;
		}
		$page = $p->show();
		// 获取查询参数
		$map['order_name'] = empty($order_name)?"":$order_name;
		$map['ordertype'] = $ordertype;
		$map['soundtype'] = $soundtype;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->allordernum = $db_order->where("type=4 and adminid=$adminid and status!=0")->count();
		$this->nowordernum = $db_order->where("type=4 and adminid=$adminid and status=3")->count();
		$this->getordernum = $db_order->where("type=4 and adminid=$adminid and (status=6 or status=8)")->count();

		// p($p);exit();
		$this->adminid = $adminid;
		$this->assign('orderinfo',$orderinfo);
		$this->assign('page', $page);
		$this->display();
}
//正在处理派发订单列表
public function mysendorder_list()
{
		// p($_POST);exit();
		$db_order = M("order");
		$ordertype = I('get.ordertype');
		$soundtype = I('get.soundtype');
		$order_name = I('get.order_name');
		$this->ordertype = $ordertype;
		$this->soundtype = $soundtype;
		$this->order_name = $order_name;
		$where = "type=4 ";
		if (!empty($soundtype)) {$where .= " and category = \"".($soundtype)."\" ";}
		if (!empty($order_name)) {$where .= " and (id like '%$order_name%' or title like '%$order_name%')";}
		if ($ordertype == 1) {$where .= " and isimportant=1 ";}
		// echo $where;exit();
		$adminid = urlsafe_b64decode(cookie('heardadminData'));
		$where .= " and status=3 and adminid=$adminid";
		$count = $db_order->where($where)->count();
		$p = getpage($count, 20);
		$orderinfo = $db_order->where($where)->order("id desc")->limit($p->firstRow, $p->listRows)->select();
		foreach ($orderinfo as $key => &$value) {
			$value['pay_time'] = $db_order->where(array("id"=>$value['id']))->order("id asc")->find()['pay_time']+1800;
		}
		$page = $p->show();
		// 获取查询参数
		$map['order_name'] = empty($order_name)?"":$order_name;
		$map['ordertype'] = $ordertype;
		$map['soundtype'] = $soundtype;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->allordernum = $db_order->where("type=4 and adminid=$adminid and status!=0")->count();
		$this->nowordernum = $db_order->where("type=4 and adminid=$adminid and status=3")->count();
		$this->getordernum = $db_order->where("type=4 and adminid=$adminid and (status=6 or status=8)")->count();

		// p($p);exit();
		$this->adminid = $adminid;
		$this->assign('orderinfo',$orderinfo);
		$this->assign('page', $page);
		$this->display();
}
//派发订单列表
public function sendorder_list()
{
		// p($_POST);exit();
		$db_order = M("order");
		$ordertype = I('get.ordertype');
		$soundtype = I('get.soundtype');
		$order_name = I('get.order_name');
		$this->ordertype = $ordertype;
		$this->soundtype = $soundtype;
		$this->order_name = $order_name;
		$where = "type=4 ";
		if (!empty($soundtype)) {$where .= " and category = \"".($soundtype)."\" ";}
		if (!empty($order_name)) {$where .= " and (id like '%$order_name%' or title like '%$order_name%')";}
		if ($ordertype == 1) {$where .= " and isimportant=1 ";}
		// echo $where;exit();
		$adminid = urlsafe_b64decode(cookie('heardadminData'));
		$where .= " and (status=6 or status=8) and adminid=$adminid";
		$count = $db_order->where($where)->count();
		$p = getpage($count, 20);
		$orderinfo = $db_order->where($where)->order("id desc")->limit($p->firstRow, $p->listRows)->select();
		foreach ($orderinfo as $key => &$value) {
			$value['pay_time'] = $db_order->where(array("id"=>$value['id']))->order("id asc")->find()['pay_time']+1800;
		}
		$page = $p->show();
		// 获取查询参数
		$map['order_name'] = empty($order_name)?"":$order_name;
		$map['ordertype'] = $ordertype;
		$map['soundtype'] = $soundtype;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->allordernum = $db_order->where("type=4 and adminid=$adminid and status!=0")->count();
		$this->nowordernum = $db_order->where("type=4 and adminid=$adminid and status=3")->count();
		$this->getordernum = $db_order->where("type=4 and adminid=$adminid and (status=6 or status=8)")->count();

		// p($p);exit();
		$this->adminid = $adminid;
		$this->assign('orderinfo',$orderinfo);
		$this->assign('page', $page);
		$this->display();
}

	//处理派单
	public function getsendorder()
	{
		// p($_GET);exit();
		$oid = I('get.oid');
		$db_order = M("order");
		$data = $db_order->where(array('id'=>$oid,'adminid'=>0))->find();
		$adminid = urlsafe_b64decode(cookie('heardadminData'));
		// p($adminid);exit();
		if ($data&&$adminid) {
			$db_order->where(array('id'=>$oid))->save(array('adminid'=>$adminid,'status'=>3,'operating_time'=>time()));
			$this->adminlog("处理派单:".$data['id'],1);
			$this->success('抢单成功',"",1);
		}else{
			$this->error('出手慢了，抢单失败',"/Home/Sendorder/sendorder_list",1);
		}
	}
//订单详情
public function sendorder_message()
{
		$oid = I('get.oid');
		$this->p = I('get.p');
		$db_order = M("order t");
		$orderdata = $db_order
							->join('__SENDORDERSCORE__ s ON t.id = s.oid',LEFT)
							->join('__MEMBER__ m ON t.user_id = m.uuid',LEFT)
							->field('t.*,IFNULL(s.type,0) scoretype,m.username,m.nickname,s.message')
							->where(array("t.id"=>$oid))->find();
		$articledata = $this->GetOss($orderdata['content']);
		if (!empty($orderdata['audio_link'])) {
			if (strpos($orderdata['audio_link'], "audio.heardtech.com") === false) {
				$orderdata['audio_link'] = "http://audio.heardtech.com/".$orderdata['audio_link'];
			}
		}
		// p($articledata);exit();
		$this->assign('orderdata',$orderdata);
		$this->assign('articledata',$articledata);
		$this->display();
}
// 处理腾讯科技订单
public function TengxunJson()
{
	// p($_POST);exit();
	$data['url'] = "http://audio.heardtech.com/".I('post.audio_mp3');
	$data['article_id'] = I('post.article_id');
	$adminid = urlsafe_b64decode(cookie('heardadminData'));
	$adminname = M("admin")->where(array("heard_admin_id"=>$adminid))->find()['heard_adminname'];
	if (!$adminname) {$this->ApiError('提交失败，请重新登录','999990');}
	$data['anchor_id'] = substr($adminname, -4);
	$data['sex'] = M("admin")->where(array("heard_admin_id"=>$adminid))->find()['sex'];
	$data['date'] = date("Ymd",time());
	$res1 = M('tencent')->add($data);
	$where['id'] = I('post.oid');
	$data2['status'] = 6;
	$data2['audio_link']     = I('post.audio_mp3');
	$data2['audio_upload_time'] = time();
	$res2 = M('order')->where($where)->save($data2);
	if ($res1&&$res2) {
		$this->ApiSuccess("提交成功");
	}else{
		$this->ApiError('提交失败，请联系管理员','999990');
	}
}
//派单抽审列表
public function sendcheck_list()
{
		// p($_POST);exit();
		$db_order = M("order t");
		$soundtype = I('get.soundtype');
		$order_name = I('get.order_name');
		$this->p = I('get.p')?I('get.p'):1;
		$this->soundtype = $soundtype;
		$this->order_name = $order_name;
		$where = "(t.type=3 or t.type=4) ";
		if (!empty($soundtype)) {$where .= " and t.category = \"".($soundtype)."\" ";}
		if (!empty($order_name)) {$where .= " and (id like '%$order_name%' or title like '%$order_name%')";}
		// echo $where;exit();
		$where .= " and t.adminid=0 and (t.status=6 or t.status=8)";
		$count = $db_order
							->join('__SENDORDERSCORE__ s ON t.id = s.oid',LEFT)
							// ->join('__SENDORDERSCORE__ s ON t.media_id = s.id',LEFT)
							->join('__MEMBER__ m ON t.user_id = m.uuid',LEFT)
							->field('t.id')
							->where($where)->count();
		$p = getpage($count, 20);
		$orderinfo = $db_order
							->join('__SENDORDERSCORE__ s ON t.id = s.oid',LEFT)
							->join('__MEMBER__ m ON t.user_id = m.uuid',LEFT)
							->field('t.*,IFNULL(s.type,0) scoretype,m.username')
							->where($where)->order("id desc")->limit($p->firstRow, $p->listRows)->select();
		/*foreach ($orderinfo as $key => &$value) {
			if (empty($value['type'])) {
				$value['type'] = 0;
			}
		}*/
		$page = $p->show();
		// 获取查询参数
		$map['order_name'] = empty($order_name)?"":$order_name;
		$map['soundtype'] = $soundtype;
		$p->parameter = "";
		foreach($map as $key=>$val) {
			$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($orderinfo);exit();
		$adminid = urlsafe_b64decode(cookie('heardadminData'));
		$this->adminid = $adminid;
		$this->assign('orderinfo',$orderinfo);
		$this->assign('page', $page);
		$this->display();
}
	//处理派单
	public function check_success()
	{
		$oid = I('get.oid');
		$p = I('get.p');
		$db_sendorderscore = M("sendorderscore");
		if ($oid) {
			$db_sendorderscore->add(array('oid'=>$oid,'message'=>"",'type'=>1,'time'=>date("Y-m-d H:i:s",time()),'adminid'=>urlsafe_b64decode(cookie('heardadminData'))));
			$this->adminlog("处理派单:".$oid);
			$this->success('处理成功',"/Home/Sendorder/sendcheck_list?p=".$p,1);
		}else{
			$this->error('处理失败',"",1);
		}
	}
	//处理派单
	public function check_fail()
	{
		// p($_POST);exit();
		$oid = I('post.oid');
		$p = I('post.p');
		$message = I('post.fail_message');
		$db_sendorderscore = M("sendorderscore");
		if ($oid&&$message) {
			$db_sendorderscore->add(array('oid'=>$oid,'message'=>$message,'type'=>2,'time'=>date("Y-m-d H:i:s",time())));
			$this->adminlog("评价派单:".$oid);
			$this->success('评价成功',"/Home/Sendorder/sendcheck_list?p=".$p,1);
		}else{
			$this->error('评价失败',"",1);
		}
	}

/*-----------------------*/
}